package com.imooc.controller;

import com.imooc.service.impl.StuServiceImpl;
import com.imooc.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//@Controller
@ApiIgnore  // Swagger2的显示中忽略该 Controller
@RestController
@RequestMapping(value = "redis")
public class RedisController {


    @Autowired
    private RedisOperator redisOperator;
    // 注入Redis模板类
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/set")
    public Object set(String key,String value){
        // opsForValue 针对String内容的操作
        redisTemplate.opsForValue().set(key,value);
        return "ok";
    }

    @GetMapping("/get")
    public Object get(String key){
        return redisTemplate.opsForValue().get(key);
    }

    @GetMapping("/delete")
    public Object delete(String key){
        // 针对所有的类型都可以进行删除
        redisTemplate.delete(key);
        return "ok";
    }

    /**
     * 查询大量的Key
     * @param keys key数组
     * @return 查询结果
     */
    @GetMapping("/getAlot")
    public Object getALot(String... keys){
        List<String> result = new ArrayList<>();
        for(String k :keys){
            result.add(redisOperator.get(k));
        }
        return "ok";
    }

    /**
     * 批量查询
     * @param keys key数组
     * @return 查询结果
     */
    @GetMapping("/mget")
    public Object mget(String... keys){
        List<String> keysList = Arrays.asList(keys);
        return redisOperator.mget(keysList);
    }

    /**
     * 使用管道进行批量查询
     * @param keys key数组
     * @return 查询结果
     */
    @GetMapping("/batchGet")
    public Object batchGet(String... keys){
        List<String> keysList = Arrays.asList(keys);
        return redisOperator.batchGet(keysList);
    }




}
